
// File Name: rs485_ctrl.h
// Author: kukukukuku~
// Date: 2024-07-16

#ifndef __RS485_CTRL_H
#define __RS485_CTRL_H

#include "at32f421.h"

#define RS485_GPIO_CRM                      CRM_GPIOA_PERIPH_CLOCK
#define RS485_DE_RE_GPIO_PORT               GPIOA
#define RS485_DE_RE_GPIO_PIN                GPIO_PINS_1
#define RS485_USART_TX_GPIO_PORT            GPIOA
#define RS485_USART_TX_GPIO_PIN             GPIO_PINS_2
#define RS485_USART_RX_GPIO_PORT            GPIOA
#define RS485_USART_RX_GPIO_PIN             GPIO_PINS_3
#define RS485_USART_TX_GPIO_MUX_SOURCE      GPIO_PINS_SOURCE2
#define RS485_USART_TX_GPIO_MUX             GPIO_MUX_1
#define RS485_USART_RX_GPIO_MUX_SOURCE      GPIO_PINS_SOURCE3
#define RS485_USART_RX_GPIO_MUX             GPIO_MUX_1

#define RS485_USART_CRM                     CRM_USART2_PERIPH_CLOCK
#define RS485_USART                         USART2
#define RS485_USART_BAUDRATE                (115200)
#define RS485_USART_DATA                    USART_DATA_8BITS
#define RS485_USART_STOP                    USART_STOP_1_BIT
#define RS485_USART_PARITY                  USART_PARITY_NONE
#define RS485_USART_IRQ                     USART2_IRQn
#define RS485_USART_HANDLE                  USART2_IRQHandler

#define RS485_MODE_TX                       gpio_bits_set(RS485_DE_RE_GPIO_PORT, RS485_DE_RE_GPIO_PIN)
#define RS485_MODE_RX                       gpio_bits_reset(RS485_DE_RE_GPIO_PORT, RS485_DE_RE_GPIO_PIN)

#define RS485_USART_TX_BUFFER_LEN           (64)
#define RS485_USART_RX_BUFFER_LEN           (64)

#define RS485_MODBUS_READ_SINGLE_REG_CODE   (0x11)
#define RS485_MODBUS_WRITE_SINGLE_REG_CODE  (0x12)

#define RS485_REG_SAVE                      (0x00)
#define RS485_REG_REBOOT                    (0x01)
#define RS485_REG_RESET                     (0x02)

#define RS485_REG_MODBUS_MODE               (0x10)
#define RS485_REG_MODBUS_ID                 (0x11)
#define RS485_REG_MODBUS_BAUD_H             (0x12)
#define RS485_REG_MODBUS_BAUD_L             (0x13)
#define RS485_REG_MODBUS_ACTIVE_INTERVAL    (0x14)

#define RS485_REG_KEY_CLICK_BEEP            (0x20)
#define RS485_REG_KEY_LONG_BEEP             (0x21)

#define RS485_REG_LED1_MODE                 (0x30)
#define RS485_REG_LED2_MODE                 (0x31)
#define RS485_REG_LED1_BREATH_STEP          (0x32)
#define RS485_REG_LED2_BREATH_STEP          (0x33)
#define RS485_REG_LED1_BLINK_FREQ           (0x34)
#define RS485_REG_LED2_BLINK_FREQ           (0x35)
#define RS485_REG_LED1_SWITCH               (0x36)
#define RS485_REG_LED2_SWITCH               (0x37)

#define RS485_REG_OLED_DIR                  (0x40)
#define RS485_REG_OLED_LIGHT                (0x41)
#define RS485_REG_OLED_CURSOR               (0x42)
#define RS485_REG_OLED_REFRESH_MS           (0x43)
#define RS485_REG_OLED_MENU_NUMBER          (0x44)

#define RS485_REG_STEP_MOTOR_STATE          (0x50)
#define RS485_REG_STEP_MOTOR_MODE           (0x51)
#define RS485_REG_STEP_MOTOR_SPD            (0x52)

#define RS485_REG_WS2812B_MODE              (0x60)
#define RS485_REG_WS2812B_STOP_R            (0x61)
#define RS485_REG_WS2812B_STOP_G            (0x62)
#define RS485_REG_WS2812B_STOP_B            (0x63)
#define RS485_REG_WS2812B_CW_MODE           (0x64)
#define RS485_REG_WS2812B_CW_R              (0x65)
#define RS485_REG_WS2812B_CW_G              (0x66)
#define RS485_REG_WS2812B_CW_B              (0x67)
#define RS485_REG_WS2812B_CCW_MODE          (0x68)
#define RS485_REG_WS2812B_CCW_R             (0x69)
#define RS485_REG_WS2812B_CCW_G             (0x6A)
#define RS485_REG_WS2812B_CCW_B             (0x6B)

#define RS485_REG_TEMPERATURE               (0x70)
#define RS485_REG_V_REF                     (0x71)
#define RS485_REG_VERSION_H                 (0x72)
#define RS485_REG_VERSION_L                 (0x73)

#define RS485_MODE                          (0)
#define RS485_ID                            (0x01)
#define RS485_ID_MIN                        (0x01)
#define RS485_ID_MAX                        (0xFF)
#define RS485_ACTIVE_INTERVAL               (1000)
#define RS485_ACTIVE_INTERVAL_MIN           (1000)
#define RS485_ACTIVE_INTERVAL_MAX           (5000)

extern const uint32_t rs485UsartBaudRateList[10]; //RS485串口支持的波特率列表
extern uint32_t rs485UsartBaudRate;    //RS485串口波特率
extern uint32_t rs485UsartBaudRateNew; //RS485新串口波特率

extern uint16_t rs485ActiveInterval;   //RS485主动模式发送时间间隔

extern uint8_t rs485UsartTxBuffer[RS485_USART_TX_BUFFER_LEN];  //RS485串口发送缓冲区
extern uint8_t rs485UsartRxBuffer[RS485_USART_RX_BUFFER_LEN];  //RS485串口接收缓冲区
extern uint8_t rs485UsartRxIndex;      //RS485串口接收下标
extern uint8_t rs485UsartRxFinishFlag; //RS485串口接收完成标志

extern uint8_t rs485Mode;  //RS485工作模式
extern uint8_t rs485Id;    //RS485设备编号

extern uint8_t rs485SaveFlag;  //RS485接收到保存命令标志
extern uint8_t rs485RebootFlag;    //RS485接收到重启命令标志
extern uint8_t rs485ResetFlag;     //RS485接收到恢复出厂设置命令标志

//RS485配置
void rs485_config(void);

#endif //__RS485_CTRL_H
